/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.engagemangsindex.ejb;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

/**
 *
 * @author khaleddaham
 */
@Singleton 
@Startup
@Slf4j
public class LogicalAddressCache {

    private List<String> addressCache = null;

    /**
     *
     * @param addressList
     */
    public void updateCache(List<String> addressList) {
        synchronized (this) {
            if (addressList != null) {
                log.info("Adding {} logical addresses to the cache.", addressList.size());
                reset();
                addressCache = addressList;
            }
        }
    }

    /**
     *
     */
    protected void reset() {
        addressCache = null;
    }

    /**
     *
     * @return
     */
    public List<String> getLogicalAddresses() {
        List<String> addressList = new ArrayList<String>();
        synchronized (this) {
            if (addressCache != null) {
                addressList.addAll(this.addressCache);
            }
        }
        return addressList;
    }
    
    public void buildRoutes() {
            for (String addr : addressCache) {
                // Build routes.
                try {
                    CamelContext context = new DefaultCamelContext();
                    context.addRoutes(new RouteBuilder() {
                        @Override
                        public void configure() {
                            from("test-jms:queue:test.queue").to("file://test");
                        }
                    });
                } catch (Exception ex) {
                    log.warn(addr, ex);
                }
                // Build up jms queues.
            }
    }
}